<?php
namespace app\serverintegralmall\logic;
use \think\Db;
use \app\manage\logic\BaseLogic;
use \app\manage\service\ApiBase;


class Goods extends BaseLogic{
	
  protected $name = 'goods';
	protected $config = 'db_config_integralmall';
	protected $goodsModel = 'app\serverintegralmall\model\Goods';
  protected $goodsLogic = '\app\serverintegralmall\logic\Goods';

    /*内部根据id进行简单直接的db查询商品列表*/
    public function dbGoodsList($data)
    {
        return $this->db()
                    ->where('id','in',$data['id'])
                    ->where('is_del','neq',2)
                    ->select();
    }

    /*艺货后台发布积分商品*/
    public function addIntegralGoods($data): array 
    {
    	$data['number'] = $data['origin_number'];
    	$data['status'] = 1;
    	$imgUrls = explode(",", $data['imgs_urls']);
    	!$data['profile_pic'] && $data['profile_pic'] = $imgUrls[0];
   		try{
   			$res = $this->model($this->goodsModel)->allowField(['user_id', 'cate_id', 'title', 'integral', 'product_code', 'profile_pic', 'imgs_urls', 'imgs_ids', 'origin_number', 'order', 'desc', 'status', 'number'])->save($data);
   		}catch(\Exception $e){
   			return server_err(null, $e->getMessage());
   		}
    	return auto_return($res);
    }

    /*艺货后台修改积分商品*/
    public function updateIntegralGoods($data): array 
    {
      $_where['id'] = $data['id'];
      $_where['is_del'] = 0;
      $info = $this->model($this->goodsModel)->where($_where)->find();
      $number = isset($data['number']) ? $data['number'] : $info['number'];
      $origin_number = isset($data['origin_number']) ? $data['origin_number'] : $info['origin_number'];
      if($number > $origin_number){
        return verify_err(null, '剩余数量不能大于原始数量');
      }
      $res = $this->model($this->goodsModel)->allowField(['cate_id', 'user_id', 'title', 'integral', 'profile_pic', 'imgs_urls', 'imgs_ids', 'number', 'origin_number', 'order', 'desc'])->save($data, $_where);
      return auto_return($res, '成功修改该商品', '修改商品失败');
    }

    /*后台获取积分商品列表数据*/
    public function allGoodsList($data): array 
    {
      $_where['is_del'] = 0;
      $data['keywords'] && $_where['goods.title'] = ['like', '%' . $data['keywords'] . '%'];
      $manageAppId = ApiBase::getAppId();
      $_whereOr = $manageAppId == $data['app_id'] ? true : 'goods_category.`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR goods_category.is_public = 1';
      $list = $this->model($this->goodsModel)
                ->alias('goods')
                ->field('goods.*,goods_category.name as cate_name')
                ->join('goods_category', 'goods_category.id = goods.cate_id', 'LEFT')
                ->where($_where)
                ->where($_whereOr)
                ->order('goods.id desc')
                ->paginate(null, false, ['page'=>$data['page'], 'list_rows'=>$data['listRows']])
                ->toArray();
      return auto_return_page($list);
    }

    /*管理员批量操作（审核、下架）商品*/
    public function batchOperaGoods($data, $operate): array 
    {
        //左链接分类表，对app_id进行验证操作
        $manageAppId = ApiBase::getAppId();
        $_whereOr = $manageAppId == $data['app_id'] ? true : 'goods_category.`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR goods_category.is_public = 1';

        $_where['goods.id'] = ['in',$data['id']];
        $_where['goods.is_del'] = 0;

        if($operate == 'manageBatchSoldOutGoods'){
            $_where['goods.status'] = ['in',['1','4']];
        }

        $saveData['goods.status'] = $data['status'];
        $res = $this->model($this->goodsModel)
                ->alias('goods')
                ->join('goods_category', 'goods_category.id = goods.cate_id', 'LEFT')
                ->where($_where)
                ->where($_whereOr)
                ->update($saveData);

        return auto_return($res);
    }

    /*管理员删除一个或多个商品*/
    public function manageDelGoods($data): array 
    {

        !is_array($data) && $data['id'] = [$data['id']]; 
        $_where['goods.id'] = ['in',$data['id']];
        $_where['goods.is_del'] = 0;
        $saveData['goods.is_del'] = 1;

        //左链接分类表，对app_id进行验证操作
        $manageAppId = ApiBase::getAppId();
        $_whereOr = $manageAppId == $data['app_id'] ? true : 'goods_category.`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR goods_category.is_public = 1';

        $res = $this->db()
                    ->alias('goods')
                    ->join('goods_category', 'goods_category.id = goods.cate_id', 'LEFT')
                    ->where($_where)
                    ->where($_whereOr)
                    ->update($saveData);

        return auto_return($res);
    }
    /*前台获取积分商品分页列表*/
    public function frontGoodsList($data): array 
    {
      $_where['goods.is_del'] = 0;
      $_where['goods.status'] = 1;
      $_where['goods.number'] = ['gt', 0];
      $data['keywords'] && $_where['goods.title'] = ['like', '%' . $data['keywords'] . '%'];
      $manageAppId = ApiBase::getAppId();
      $_whereOr = $manageAppId == $data['app_id'] ? true : 'goods_category.`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR goods_category.is_public = 1';
      $list = $this->model($this->goodsModel)
                ->alias('goods')
                ->field('goods.*,goods_category.name as cate_name')
                ->join('goods_category', 'goods_category.id = goods.cate_id', 'LEFT')
                ->where($_where)
                ->where($_whereOr)
                ->paginate(null, false, ['page'=>$data['page'], 'list_rows'=>$data['listRows']])
                ->toArray();
      return auto_return_page($list);
    }

    /*前台获取某积分商品的详情*/
    public function frontGoodsInfo($data): array 
    {
      $_where['goods.number'] = ['gt', 0];
      $_where['goods.status'] = 1;
      $_where['goods.is_del'] = 0;
      $_where['goods.id'] = $data['id'];
      $manageAppId = ApiBase::getAppId();
      $_whereOr = $manageAppId == $data['app_id'] ? true : 'goods_category.`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR goods_category.is_public = 1';
      $info = $this->model($this->goodsModel)
                  ->alias('goods')
                  ->field('goods.*,goods_category.name as cate_name')
                  ->join('goods_category', 'goods_category.id = goods.cate_id', 'LEFT')
                  ->where($_where)
                  ->where($_whereOr)
                  ->find();
      return auto_return($info);
    }










}